﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DotNet.Business;
using System.Collections.Generic;
using System.Text;
public partial class admin_editProduct : System.Web.UI.Page
{
    #region 初始化变量
    tab_productBll productBll = new tab_productBll();
    tab_productClassBll productClassBll = new tab_productClassBll();
    StringBuilder sb = new StringBuilder();
    private int productID = 0;
    private int pcid = 0;
    #endregion
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["pid"]!=null)
            {
                this.hid_id.Value = Request.QueryString["pid"];
                productID = Convert.ToInt32(Request.QueryString["pid"]);
                if (Request.QueryString["action"] != null)
                {
                    string action = Request.QueryString["action"].ToLower().Trim();
                    switch (action)
                    {
                        case "update":
                            Update();
                            break;
                        default:
                            break;
                    }
                }
                bindProduct();
            }           
        }
    }

    #region 绑定产品信息
    private void bindProduct() 
    {
        tab_product productModel = new tab_product();
        productModel = productBll.GetModel(productID);
        this.txt_productName.Value = productModel.productName;    
        this.pcid = Convert.ToInt32(productModel.pcid.ToString());
        this.FCKeditor1.Value = productModel.productInfo;
        this.txt_title.Value = productModel.productTitle;
        this.txt_key.Value = productModel.productKey;
        this.txt_seoDescription.Value = productModel.productDescription;
        this.productID = Convert.ToInt32(productModel.pid);
    }
    #endregion

    #region 产品类别
    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    public string ProductClassSelect()
    {
        IList<tab_productClass> Ilist_ProductClass = productClassBll.GetModel();
        foreach (tab_productClass productClass in Ilist_ProductClass)
        {
            if (productClass.pcid == pcid)
            {
                sb.AppendFormat("<option selected='true' value='{0}'>{1}</option>", productClass.pcid, productClass.productClassName);
            }
            else
            {
                sb.AppendFormat("<option value='{0}'>{1}</option>", productClass.pcid, productClass.productClassName);
            }            
        }
        return sb.ToString();
    }
    #endregion

    #region 修改产品信息
    private void Update() 
    {
        tab_product productModel = new tab_product();
        productModel.pid = productID;
        productModel.productName = Request.Form["txt_productName"].ToString();
        if (productModel.productName.Trim()=="")
        {
            Response.Write("没有添加产品名称！");
            Response.End();
        }
        productModel.productInfo =Server.HtmlDecode(Request.Form["FCKeditor1"].ToString());
        productModel.productTitle = Request.Form["txt_title"].ToString();
        productModel.productKey = Request.Form["txt_key"].ToString();
        productModel.productDescription = Request.Form["txt_seoDescription"].ToString();
        productModel.createDate = DateTime.Now;
        productModel.pcid = Convert.ToInt32(Request.Form["sel_productClass"]);
        if (productModel.pcid<=0)
        {
            Response.Write("没有选择产品类别！");
            Response.End(); 
        }
        if (productBll.Update(productModel))
        {
            Response.Write("1");
            Response.End();
        }
        else
        {
            Response.Write("更新失败!");
            Response.End(); 
        } 
    }
    #endregion

}
